import { Component, OnInit, Input, Injector } from '@angular/core';
import { HttpClient, HttpRequest, HttpResponse } from "@angular/common/http";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { debounceTime } from 'rxjs/operators/debounceTime';
import { map } from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
import { ActivatedRoute, NavigationExtras } from '@angular/router';
import { NzMessageService, NzModalRef, NzModalService, UploadFile } from 'ng-zorro-antd';
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { environment } from 'environments/environment';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { StudentJoinCourseOutput } from '../../../core/model/StudentJoinCourseDto';
import { isNumber } from 'util';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-student-join-course',
  templateUrl: './my-drill.component.html',
  styleUrls: ['../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss', './my-drill.component.scss']
})
export class MyDrillComponent extends NgZorroPageBaseComponent implements OnInit {
  listUrl = 'ClassInfo/GetClassByName';
  searchChange$ = new BehaviorSubject(new SearchValue('', ''));
  optionList = [];
  selectedUser;
  // 开始演练
  startDrillId;
  confirmModal: NzModalRef;
  /**
   * 是否从远程获取数据
   */
  isLoading = false;
  //  上传
  uploading = false;
  fileList: UploadFile[] = [];
  //
  onSearch(value: string, url: string): void {
    this.isLoading = true;
    this.searchChange$.next(new SearchValue(value, this.listUrl));
  }

  constructor(
    injecter: Injector,
    private modal: NzModalService,
    public httpService: HttpService,
    public http: HttpClient) {
    super(injecter,
      new BaseComponentConfig(
        "DrillRecord/GetPagerDrillRecord",
        "",
        "",
        "",
        "",
        "id",
        "",
        "我的演练",
        [],
        "Id",
        { Edit: false, Dell: false },
      ));
    this.listEntities.pageSize = 12;
  }
  async toAddDrillRecord() {
    const canAddDrillRecord = await this.httpService.get<any>('DrillRecord/CanAddDrillRecord');
    if (canAddDrillRecord) {
      this.router.navigate(['app/mydrill/adddrill']);
    } else {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '您有未完成的实训，不能创建新的实训。'
      });

    }
    // this.router.navigate(['app/mydrill/adddrill']);
  }
  ngOnInit() {
    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });

  }
  // 开始演练
  async startDrill(id, drillType) {
    this.startDrillId = id;
    this.confirmModal = this.modal.confirm({
      nzTitle: '是否要开始演练?',
      nzContent: '点击确定后，此演练的信息将不能更改。',
      nzOnOk: async () => {

        const startpar = { Id: this.startDrillId };
        const result = await this.httpService.post<any>('DrillRecord/StartDrillRecord', startpar);
        const navigationExtras: NavigationExtras = {
          queryParams: { 'id': id }
        };
        this.router.navigate(['/mydrillcontent'], navigationExtras);
        // location.reload();
      },
      nzOnCancel: () => {

      }
    });
  }
  // 跳转到演练步骤
  async toDrillStep(value: any) {
    const navigationExtras: NavigationExtras = {
      queryParams: { 'id': value.id }
    };
    this.router.navigate(['/mydrillcontent'], navigationExtras);
  }
  // 上传演练报告
  async toUploadReport(value: any) {
    this.router.navigate(['app/mydrill/reportdrill', value.id]);
    // this.confirmModal = this.modal.confirm({
    //   nzTitle: '上传演练报告?',
    //   nzContent: '点击确定后，此演练的信息将被删除。',
    //   nzOnOk: () => {

    //   },
    //   nzOnCancel: () => {

    //   }
    // });
  }
  // beforeUpload = (file: UploadFile): boolean => {
  //   this.fileList.push(file);
  //   return false;
  // }

  // async handleUpload(id) {
  //   const formData = new FormData();
  //   // tslint:disable-next-line:no-any
  //   formData.append('drillRecordId', id);
  //   this.fileList.forEach((file: any) => {
  //     formData.append('fileinput', file);
  //   });
  //   this.uploading = true;
  //   // You can use any AJAX library you like
  //   // const startpar = { Id: this.startDrillId };
  //   const req = await this.httpService.post<any>('DrillRecord/UploadReport', formData);
  // }
  // 编辑演练
  async editDetail(value: any) {
    if (value.drillStatus !== 0) {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '演练当前状态不可编辑。'
      });
      return;
    } else {
      this.router.navigate(['app/mydrill/editdrill', value.id]);
    }

  }
  // 删除演练
  async deleteDrill(id) {
    this.confirmModal = this.modal.confirm({
      nzTitle: '是否要删除演练?',
      nzContent: '点击确定后，此演练的信息将被删除。',
      nzOnOk: async () => {
        await this.httpService.get<any>('DrillRecord/DeleteDrillRecord?id=' + id);
        this.getEntityList();
      },
      nzOnCancel: () => {

      }
    });
  }

  random() {
    return Math.ceil(Math.random() * 4);
  }
}
